//
//  CommenWebViewController.m
//  NoNameAmazing
//
//  Created by hero on 2017/9/6.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//

#import "CommenWebViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
@protocol JSObjcDelegate <JSExport>
//tianbai对象调用的JavaScript方法，必须声明！！！
- (void)getUserId;
//- (void)getCall:(NSString *)callString;

@end
@interface CommenWebViewController ()<UIWebViewDelegate,JSObjcDelegate>
@property (nonatomic ,strong) UIWebView *web;
@property (nonatomic, strong) JSContext *context;
@property (nonatomic, weak) UIProgressView *progressView;
@end

@implementation CommenWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self requestwb];
    [self prepareUI];
//    [self prepareProgressView];
    
}
/**
 设置进度条
 */
- (void)prepareProgressView {
    
    UIProgressView *progressView = [UIProgressView new];
    _progressView = progressView;
    [self.view addSubview:progressView];
    progressView.tintColor = [UIColor blueColor];
    
    progressView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:progressView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:progressView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:progressView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:progressView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:2]];
}
- (void)prepareUI{
    if ([self.str isEqualToString:@"home"]) {
//        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
        self.web =[[UIWebView alloc]initWithFrame:self.view.frame];
        [self.web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]]];
//        [self.view insertSubview:self.web belowSubview:_progressView];
        [self.view addSubview:self.web];
    }else{
    
        self.web =[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64)];
        self.web.delegate = self;
        self.context = [self.web valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
        self.context[@"jsCallOC"] = self;
        //[self getUserId];
        self.context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
            context.exception = exceptionValue;
            NSLog(@"异常信息：%@", exceptionValue);
        };


        [self.web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.urlStr]]]];

        [self.view addSubview:self.web];
    }
    
}

//- (void)webViewDidFinishLoad:(UIWebView *)webView
//{
//    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//        self.context[@"jsCallOC"] = self;
//   //[self getUserId];
//    self.context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
//        context.exception = exceptionValue;
//        NSLog(@"异常信息：%@", exceptionValue);
//    };
//    
//}

- (void)getUserId{
    NSLog(@"getUserId");
    // 之后在回调JavaScript的方法Callback把内容传出去
    JSValue *Callback = self.context[@"Callback"];
    //传值给web端
    [Callback callWithArguments:@[[us objectForKey:kUserid]]];

}


//-(void)requestwb
//{
//    NSDictionary *params = @{@"userId":[us objectForKey:kUserid]
//                                                    };
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//    manager.responseSerializer = [ AFHTTPResponseSerializer serializer];
//    //设置返回类型
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
////    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
//    [manager POST:self.urlStr parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
//        NSLog(@"uploadProgress %@", uploadProgress);
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSString *result = [[NSString alloc] initWithData:responseObject  encoding:NSUTF8StringEncoding];
//        UIWebView *webView = [[UIWebView alloc] init];
//        webView.frame = self.view.bounds;
//        
//        [self.view addSubview: webView];
//        [webView loadHTMLString:result baseURL:[NSURL URLWithString:self.urlStr]];
//        NSLog(@"result");
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"faile %@", error);
//        
//        
//    }];
//}


@end
